#!/usr/bin/env python

import pygtk
pygtk.require("2.0")

import gtk

from note_buffer import NoteBuffer

class SearchWindow:
    __wnd__                  = gtk.Window(gtk.WINDOW_TOPLEVEL)
    __txt_find__             = gtk.Entry()
    __txt_replace__          = gtk.Entry()
    __chk_case__             = gtk.CheckButton("_Case senitive", True)
    __chk_entire__           = gtk.CheckButton("Match _entire words only", True)
    __chk_backwards__        = gtk.CheckButton("Search _backwards", True)
    __chk_wrap__             = gtk.CheckButton("_Wrap around", True)
    __note_window__          = None

    def find(self, widget):
        str_find = self.__txt_find__.get_text()
        p = self.__note_window__.get_current_page()
        box = self.__note_window__.get_nth_page(p)

        if str_find == "" or box is None:
            return

        buffer = box.get_children()[0].get_buffer()

        buffer.find(str_find, self.__chk_backwards__.get_active(),
                    self.__chk_wrap__.get_active())

    def find_replace_next(self, widget):
        str_find = self.__txt_find__.get_text()
        str_replace = self.__txt_replace__.get_text()
        p = self.__note_window__.get_current_page()
        box = self.__note_window__.get_nth_page(p)

        if str_find == "" or box is None:
            return

        buffer = box.get_children()[0].get_buffer()

        buffer.replace(str_find, str_replace,
                       self.__chk_backwards__.get_active(),
                       self.__chk_wrap__.get_active())

    def find_replace_all(self, widget):
        str_find = self.__txt_find__.get_text()
        str_replace = self.__txt_replace__.get_text()
        p = self.__note_window__.get_current_page()
        box = self.__note_window__.get_nth_page(p)

        if str_find == "" or box is None:
            return

        buffer = box.get_children()[0].get_buffer()

        buffer.replaceAll(str_find, str_replace)

    def close(self, widget):
        self.hide()

    def __init__(self, nw):
        self.__note_window__ = nw
        self.__wnd__.set_size_request(525, 250)
        self.__wnd__.set_position(gtk.WIN_POS_CENTER)
        self.__wnd__.set_title("Find and replace")
        self.__wnd__.connect("delete_event", lambda w,e: gtk.main_quit())

        vbox_main = gtk.VBox(False, 10)
        self.__wnd__.add(vbox_main)

        # Put the labels in one vbox and text boxes in another to make
        # things look right
        hbox_search = gtk.HBox(False, 0)

        # Labels
        vbox_lbl = gtk.VBox(False, 0)
        lbl_find = gtk.Label("Find:")
        lbl_find.show()
        vbox_lbl.add(lbl_find)
        lbl_replace = gtk.Label("Replace:")
        lbl_replace.show()
        vbox_lbl.add(lbl_replace)
        vbox_lbl.show()
        hbox_search.add(vbox_lbl)

        # Text boxes
        vbox_txt = gtk.VBox(True, 0)
        self.__txt_find__.show()
        vbox_txt.add(self.__txt_find__)
        self.__txt_replace__.show()
        vbox_txt.add(self.__txt_replace__)
        vbox_txt.show()
        hbox_search.add(vbox_txt)
        hbox_search.show()
        vbox_main.add(hbox_search)

        # Options
        hbox_opts = gtk.VBox("False", 0)
        #self.__chk_case__.show()
        #hbox_opts.add(self.__chk_case__)
        #self.__chk_entire__.show()
        #hbox_opts.add(self.__chk_entire__)
        self.__chk_backwards__.show()
        hbox_opts.add(self.__chk_backwards__)
        self.__chk_wrap__.show()
        self.__chk_wrap__.set_active(True)
        hbox_opts.add(self.__chk_wrap__)
        hbox_opts.show()
        vbox_main.add(hbox_opts)

        # Buttons
        vbox_bnts = gtk.HBox("True", 0)
        bnt_find = gtk.Button("Find")
        bnt_find.connect("clicked", self.find)
        bnt_find.show()
        vbox_bnts.add(bnt_find)
        bnt_replace_next = gtk.Button("Replace next")
        bnt_replace_next.show()
        bnt_replace_next.connect("clicked", self.find_replace_next)
        vbox_bnts.add(bnt_replace_next)
        bnt_replace_all = gtk.Button("Replace all")
        bnt_replace_all.show()
        bnt_replace_all.connect("clicked", self.find_replace_all)
        vbox_bnts.add(bnt_replace_all)
        bnt_close = gtk.Button("Close")
        bnt_close.show()
        bnt_close.connect("clicked", self.close)
        vbox_bnts.add(bnt_close)
        vbox_bnts.show()
        vbox_main.add(vbox_bnts)

        vbox_main.show()

    def show(self):
        self.__wnd__.show()

    def hide(self):
        self.__wnd__.hide()

